import homeApi from '@/api/home.api.js'
import { mapMutations, mapActions } from 'vuex'
export default {
  data () {
    return {
      menuCodeList: [],
      routerArr: [], // 所有路由
      lastRouterArr: [], // 最后一级路由的集合
      dataScreen: null, // 数据总览
      dataScreenBill: null, //
      dataScreenSpin: true,
      badgeNum: 0,
      processMsgList: [], // 审批流程
      processMsgSpin: true,
      loggerList: [], // 操作日志
      loggerSpin: true,
      financeData: null, // 财务信息
      financeSpin: true,
      customFastMenuList: [], // 自定义选项列表
      customFastMenuListSpin: true,
      customize: false, // 自定义弹窗
      fastMenuList: [], // 快捷键菜单数组
      fastMenuListId: [], // 快捷键菜单数组Id
      fastMenuListSpin: true,
      customerStatus: 0, // 客户状态
      dataScreening: null, // 数据总览
      expirationReminder: null, // 到期提醒
      treatmentSituation: null, // 待遇情况（办理中）
      serviceOf: null, // 当月服务情况
    }
  },
  created () {
    this.routerArr = this.$router.options.routes
    this.getLastRouter(this.routerArr, this.lastRouterArr)
    this.getClientWorkBench()
    this.getDataScreen()
    this.GetDataScreenBill()
    this.getLogger()
    this.getFastMenuList()
  },
  computed: {
    hasCustomer () {
      return this.menuCodeList.includes('customerInformation')
    },
    hasRoster () {
      return this.menuCodeList.includes('roster')
    },
    hasCustomerBill () {
      return this.menuCodeList.includes('CustomerBill')
    },
    hasSS () {
      if (this.dataScreen) {
        return this.menuCodeList.includes(this.dataScreen.socialSecurity.code)
      } else {
        return false
      }
      // return true
    },
    hasAf () {
      if (this.dataScreen) {
        return this.menuCodeList.includes(this.dataScreen.providentFund.code)
      } else {
        return false
      }
    },
    hasCi () {
      if (this.dataScreen) {
        return this.menuCodeList.includes(this.dataScreen.commercialInsurance.code)
      } else {
        return false
      }
    }
  },
  methods: {
    ...mapActions([
      'handleSetHrmsg',
      'handleSetHrName',
      'handleSetHrPhone',
    ]),

    // 获取数据总览202105
    getClientWorkBench () {
      homeApi.GetClientWorkBench().then(res => {
        if (res.data.success) {
          console.log('%c 🌼🌼🌼 获取数据总览 : ', 'color:green', res.data.data)
          this.dataScreening = res.data.data.dataScreening
          this.customerStatus = res.data.data.customerStatus
          this.expirationReminder = res.data.data.expirationReminder
          this.treatmentSituation = res.data.data.treatmentSituation
          this.serviceOf = res.data.data.serviceOf
          this.handleSetHrmsg(JSON.stringify({hrName: res.data.data.hrName, hrPhone: res.data.data.hrPhone}))
          this.handleSetHrName(res.data.data.hrName)
          this.handleSetHrPhone(res.data.data.hrPhone)
          localStorage.setItem('hrName', res.data.data.hrName)
          localStorage.setItem('hrPhone', res.data.data.hrPhone)
        } else {          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },

    // 获取数据总览
    getDataScreen () {
      homeApi.GetDataScreen().then(res => {
        if (res.data.success) {
          this.dataScreen = res.data.data
          this.menuCodeList = res.data.data.menuCodeList
          this.dataScreenSpin = false
          if (this.dataScreen.changeFailList.length > 0) {
            this.badgeNum = this.dataScreen.changeFailList.length
          } else {
            this.badgeNum = 0
          }
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    // 获取账单数据
    GetDataScreenBill () {
      homeApi.GetDataScreenBill().then(res => {
        if (res.data.success) {
          this.dataScreenBill = res.data.data
          // this.menuCodeList = res.data.data.menuCodeList
          // this.dataScreenSpin = false
          // if (this.dataScreen.changeFailList.length > 0) {
          //   this.badgeNum = this.dataScreen.changeFailList.length
          // } else {
          //   this.badgeNum = 0
          // }
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    // 流程审批
    getProcessMsgList () {
      homeApi.GetProcessMsgList().then(res => {
        if (res.data.success) {
          this.processMsgList = res.data.data
          this.processMsgSpin = false
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    // 获取操作日志
    getLogger () {
      homeApi.GetLogger({
        PageIndex: '1',
        PageSize: 10
      }).then(res => {
        if (res.data.success) {
          this.loggerList = res.data.data
          this.loggerSpin = false
        } else {
          
          this.$Message.error({
            content: res.data.message,
            // duration: 0,
            closable: true
          })
        }
      })
    },
    // 获取财务信息
    getFinanceData () {
      homeApi.GetFinanceData().then(res => {
        if (res.data.success) {
          this.financeData = res.data.data
          this.financeSpin = false
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    // 获取客户状态
    getCustomerStatus () {
      const data = JSON.parse(localStorage.getItem('clientLoginInfo'))
      homeApi.GetCustomerStatus({
        CompanyID: data.data.companyId
      }).then(res => {
        if (res.data.success) {
          this.customerStatus = res.data.data
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    // 获取最后一级路由
    getLastRouter (arr, arr_2) {
      arr.forEach(item => {
        if (item.children) {
          this.getLastRouter(item.children, arr_2)
        } else {
          arr_2.push(item)
        }
      })
    },
    // 跳转相应的路由
    gotoPage (code, data) {
      let page = this.lastRouterArr.find(item => {
        if (item.meta.access) {
          return item.meta.access[0] === code
        }
      })
      if (page) {
        this.$router.push({ name: page.name, params: data })
      } else {
        this.$Message.warning('您没有访问此页面的权限')
      }
    },
    // 工作台异动跳转的(当月)
    gotoChangeFail (type, data) {
      let code = ''
      switch (type) {
        case 'SocialSecurity':
          code = 'ssChangesVerifyManage'
          break;
        case 'ProvidentFund':
          code = 'afChangesVerifyManage'
          break;
        case 'CommercialInsurance':
          code = 'ciChangesVerifyManage'
          break;
      }
      let page = this.lastRouterArr.find(item => {
        if (item.meta.access) {
          return item.meta.access[0] === code
        }
      })
      if (page) {
        this.$router.push({ name: page.name, params: data })
      } else {
        this.$Message.warning('您没有访问此页面的权限')
      }
    },
    gotoStatus (name, type, status) {
      this.$router.push({ name: name, params: { type: type, status: status } })
    },
    gotoRouter (name, data) {
      this.$router.push({ name: name, params: { type: data } })
    },
    // 到期
    gotoDue (name, due) {
      this.$router.push({ name: name, params: { isDue: due } })
    },
    // 退休
    gotoRetire (name, retire) {
      this.$router.push({ name: name, params: { IsRetire: retire } })
    },
    // 账单
    gotoBill (name, type) {
      this.$router.push({ name: name, params: { IsVerified: type } })
    },

    // 获取当月一号到当前时间
    getTime () {
      let myDate = new Date()
      let thisyear = myDate.getFullYear()
      let thismonth = myDate.getMonth() + 1
      let thisday = myDate.getDate()
      let beginTime = thisyear + '-' + this.toDouble(thismonth) + '-01'
      let endTime = thisyear + '-' + this.toDouble(thismonth) + '-' + this.toDouble(thisday)
      return [beginTime, endTime]
    },
    // 补零
    toDouble (now) {
      return now > 9 ? '' + now : '0' + now
    },

    // 打开自定义快捷键选项
    customizeShow () {
      this.customize = true
    },

    // 获取自定义快捷键
    getCustomFastMenuList () {
      this.customize = true
      homeApi.GetCustomFastMenuList().then(res => {
        let arr = []
        if (res.data.success) {
          this.customFastMenuList = res.data.data
          arr = res.data.data.filter(item => {
            return item.isChecked === true
          })
          this.fastMenuListId = arr.map(item => {
            return item.id
          })
          this.customFastMenuListSpin = false
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    // 获取快捷菜单
    getFastMenuList () {
      homeApi.GetFastMenuList().then(res => {
        if (res.data.success) {
          this.fastMenuList = res.data.data
          this.fastMenuListSpin = false
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    // 勾选自定义快捷菜单
    fastMenuListChange (e) {
      console.log(e)
    },
    // 保存快捷菜单
    saveFastMenuList () {
      homeApi.SaveFastMenuList({
        IdList: this.fastMenuListId
      }).then(res => {
        if (res.data.success) {
          this.$Message.success(res.data.message)
          this.getFastMenuList()
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    }
  }
}
